package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.PictureListItemVO;
import cn.tedu.csmall.product.service.IPictureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PictureServiceImpl implements IPictureService {
    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    AlbumMapper albumMapper;

    @Override
    public void addNew(PictureAddNewParam pictureAddNewParam) {
        //检查相册id是否存在
        QueryWrapper<Album> albumQueryWrapper = new QueryWrapper<>();
        albumQueryWrapper.eq("id", pictureAddNewParam.getAlbumId());
        Integer integer1 = albumMapper.selectCount(albumQueryWrapper);
        if (integer1 == 0) {
            String message = "此相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //相册id和url不能同时一样
        QueryWrapper<Picture> pictureQueryWrapper = new QueryWrapper<>();
        pictureQueryWrapper.eq("album_id", pictureAddNewParam.getAlbumId())
                .eq("url", pictureAddNewParam.getUrl());
        Integer integer = pictureMapper.selectCount(pictureQueryWrapper);
        if (integer != 0) {
            String message = "请勿重复添加！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        //检查完毕开始添加
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewParam, picture);
        picture.setGmtCreate(LocalDateTime.now())
                .setGmtModified(LocalDateTime.now());
        int insert = pictureMapper.insert(picture);
        if (insert != 1) {
            String message = "添加失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<PictureListItemVO> listByAlbumId(Long albumId) {
        //检查相册id是否存在
        QueryWrapper<Album> albumQueryWrapper = new QueryWrapper<>();
        albumQueryWrapper.eq("id", albumId);
        Integer integer1 = albumMapper.selectCount(albumQueryWrapper);
        if (integer1 == 0) {
            String message = "此相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //根据相册id查询列表
        List<PictureListItemVO> pictureListItemVOList = pictureMapper.selectByAlbumId(albumId);

        return pictureListItemVOList;
    }

}
